package com.future.myjavatools.core.zhoupu;

import java.math.BigDecimal;

/**
 * @author xiaopengw
 * @date 2018/8/20
 * @remark
 */
public class BigDecimalUtil {

    public final static int INT_COUNT = 16;
    public final static int FLOAT_COUNT = 4;
    public final static BigDecimal ZERO = new BigDecimal(0);
    public final static BigDecimal ONE = new BigDecimal(1);

    public static boolean validateBigDecimal(BigDecimal value) {
        return validateBigDecimal(value, INT_COUNT, FLOAT_COUNT);
    }

    /**
     * 校验 整数位 小数位 位数
     *
     * @param value
     * @param intCount   整数位 位数
     * @param floatCount 小数位 位数
     * @return
     */
    public static boolean validateBigDecimal(BigDecimal value, int intCount, int floatCount) {
        if (value == null) {
            return true;
        }
        if (intCount == 0) {
            intCount = INT_COUNT;
        }
        if (floatCount == 0) {
            floatCount = FLOAT_COUNT;
        }
        String[] split = value.toString().split("\\.");
        if (split.length == 1) {
            return split[0].length() <= intCount;
        } else if (split.length == 2) {
            return split[0].length() <= intCount && split[1].length() <= floatCount;
        }
        return true;
    }

    /**
     * 佣金比例 校验 [0,1]
     *
     * @param value
     * @return
     */
    public static boolean validateCommissionRate(BigDecimal value) {
        if (value == null) {
            return true;
        }
        return value.compareTo(ZERO) >= 0 && value.compareTo(ONE) <= 0;
    }

}
